package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.PostDto;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.PostVo;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/role")
public class RoleContorller {
    @Autowired
    private RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页", notes = "角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "postDto", value = "角色DTO对象", required = true, dataType = "PostDto"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"postDto.deptNo", "postDto.dataState", "postDto.postName"})
    public ResponseResult<PageResponse<RoleVo>> findPostVoPageResponse(
            @RequestBody RoleDto roleDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<RoleVo> pageResponse = roleService.findPostPage(roleDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    @PutMapping
    @ApiOperation(value = "角色添加", notes = "角色添加")
    @ApiImplicitParam(name = "postDto", value = "角色DTO对象", required = true, dataType = "RoleDto")
    @ApiOperationSupport(includeParameters = {"postDto.deptNo", "postDto.dataState", "postDto.postName", "postDto.remark"})
    public ResponseResult<RoleVo> createPost(@RequestBody RoleDto roleDto) {
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }


    @ApiOperation(value = "根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
     public ResponseResult<Set<String>> findCheckedResources(@PathVariable("roleId") Long roleId){
     Set<String> resources = roleService.findCheckedResources(roleId);
     return ResponseResult.success(resources);
     }


    @PatchMapping()
    @ApiOperation(value = "角色修改", notes = "角色修改")
    @ApiImplicitParam(name = "roleDto", value = "角色DTO对象", required = true, dataType = "roleDto")
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto ){
          Boolean logen= roleService.updateRole(roleDto);
           return ResponseResult.success(logen);
    }
    @PostMapping("/init-roles")
    @ApiOperation(value = "角色下拉框", notes = "角色下拉框")
    public ResponseResult<Boolean> findAll(){
       List<RoleVo> roleVos= roleService.findAll();
        return ResponseResult.success(roleVos);
    }
}